# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import json
from common.config import *
from common.helper import Helper


class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "公共交通意外保障（一年期）"

    # 生成产品数据
    def genProductInfo(self):

        plan = {
            # 包赔金额
            "airplane_death": ["200000", "400000", "600000", "800000", "1000000", "1600000", "2000000"],
            "price": ["5", "10", "15", "20", "25", "40", "50"],
        }
        for v in zip(plan["airplane_death"], plan["price"]):
            data = {

                "airplane_death": v[0],
                "price": Helper.decimalParse(v[1])
            }

            yield data

    def selectData(self, data={}):

        for item in self.genProductInfo():
            data["name"] = self.name
            data["company"] = "锦泰保险"
            data["days"] = "1y"
            data["people_count"] = "1"
            data["airplane_death"] = item["airplane_death"]
            data["price"] = item["price"]

            self.mergeTable(data)

    # 合并表格
    def mergeTable(self, data):
        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`airplane_death`,`price`)VALUES("{name}","{company}","{days}",{people_count},"{airplane_death}","{price}")'

        print(SQL.format(**data))
        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()
